package controllers

import (
	// . "github.com/beego/admin/src"
	// m "github.com/beego/admin/src/models"
	"gitee.com/tangrx/beego"
	"strings"
)

type CommonController struct {
	beego.Controller
	controllerName string
	actionName     string
	//	Templatetype string //ui template type
}

//前期准备
func (self *CommonController) Prepare() {
	//self.pageSize = 20
	controllerName, actionName := self.GetControllerAndAction()
	self.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
	self.actionName = strings.ToLower(actionName)
	self.Data["version"] = beego.AppConfig.String("version")
	self.Data["siteName"] = beego.AppConfig.String("site.name")
	self.Data["curRoute"] = self.controllerName + "." + self.actionName
	self.Data["curController"] = self.controllerName
	self.Data["curAction"] = self.actionName

}

// Json 输出
func (self *CommonController) json(d *map[string]interface{}) {
	self.Data["json"] = d
	self.ServeJSON()
	self.StopRun()
}

// 输出 消息
func (self *CommonController) ajaxMsg(status string, msg string) {
	d := make(map[string]interface{})
	d["status"] = status
	d["msg"] = msg
	self.json(&d)
}

// 加载模板
func (self *CommonController) view(tpl ...string) {
	var tplname string
	if len(tpl) > 0 {
		tplname = strings.Join([]string{tpl[0], "html"}, ".")
	} else {
		tplname = self.controllerName + "/" + self.actionName + ".html"
	}
	//self.Layout = "common/_layout.html"
	self.TplName = tplname
}

//
//func (this *CommonController) GetTemplatetype() string {
//	templatetype := beego.AppConfig.String("template_type")
//	if templatetype == "" {
//		templatetype = "easyui"
//	}
//	return templatetype
//}

//func (this *CommonController) GetTree() []Tree {
//	nodes, _ := m.GetNodeTree(0, 1)
//	tree := make([]Tree, len(nodes))
//	for k, v := range nodes {
//		tree[k].Id = v["Id"].(int64)
//		tree[k].Text = v["Title"].(string)
//		children, _ := m.GetNodeTree(v["Id"].(int64), 2)
//		tree[k].Children = make([]Tree, len(children))
//		for k1, v1 := range children {
//			tree[k].Children[k1].Id = v1["Id"].(int64)
//			tree[k].Children[k1].Text = v1["Title"].(string)
//			tree[k].Children[k1].Attributes.Url = "/" + v["Name"].(string) + "/" + v1["Name"].(string)
//		}
//	}
//	return tree
//}

//func init() {
//
//	//验证权限
//	AccessRegister()
//}
